<?php
declare (strict_types=1);

namespace app\admin\controller;

use addons\BuilderPlus\components\Column as Col;
use addons\BuilderPlus\components\Edit;
use addons\BuilderPlus\components\Search;
use addons\BuilderPlus\facade\Table;

class ProductCate extends Base
{

    public function getOptions(): array
    {
        return [
            'pk'       => 'id',
            'model'    => \app\common\model\ProductCate::class,
            'validate' => \app\admin\validate\ProductCate::class,
            'with'     => ['children'],
            'fast'     => [
                'save' => '_save'
            ]
        ];
    }

    public function index()
    {

        return Table::init($this)
            ->addColumns([
                ['title', '分类名称', fn(Col $col) => $col->align('left'), fn(Edit $e) => $e->require()],
                ['icon', '图标', fn(Col $col) => $col->width(80)->lazyImage(), fn(Edit $e) => $e->images()],
                ['sort', '排序', fn(Col $col) => $col->fastSort(), fn(Edit $e) => $e->number()],
                ['status', '状态', fn(Col $col) => $col->switch('开启|关闭', '1|0')->fixed('right')]
            ])
            ->addTopButtons(['add', 'deleteAll'])
            ->addRightButtons(['edit', 'delete'])
            ->addSearch([
                ['title', '标题筛选', fn(Search $search) => $search->condition('Like')]
            ])
            ->enableTree()
            ->fetch();
    }


    public function getCate()
    {
        $id = $this->request->param('id', 0);
        return json($this->model::getCate($id));
    }
}
